package org.eclipse.emf.index.omds.ui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.index.omds.core.query.QueryGateway;
import org.eclipse.emf.index.omds.core.query.QueryIndex;
import org.eclipse.emf.index.omds.main.Activator;
import org.eclipse.emf.index.omds.ui.content.OMSLableProvider;
import org.eclipse.emf.index.omds.ui.content.OMSResourceObject;
import org.eclipse.emf.index.omds.ui.content.OMSSelectedLabelProvider;
import org.eclipse.emf.index.omds.ui.control.ResourceLoader;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jst.j2ee.internal.actions.OpenJ2EEResourceAction;
import org.eclipse.jst.jee.ui.internal.navigator.OpenJEEResourceAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;

public class DialogBoxCreator extends FilteredItemsSelectionDialog{

	public DialogBoxCreator(Shell shell) {
		super(shell,true);
		OMSSelectedLabelProvider omsSelectedLabelProvider = new OMSSelectedLabelProvider();
		this.setDetailsLabelProvider(omsSelectedLabelProvider);
		setTitle("Open Java EE Artifact");
		setMessage("&Select an artifact to open (? = any character, * = any string):");
	}

	@Override
	protected Control createExtendedContentArea(Composite parent) {
	      return null;
	}

	@Override
	protected ItemsFilter createFilter() {
	      return new ItemsFilter() {
	         public boolean matchItem(Object item) {
	            return matches(item.toString());
	         }
	         public boolean isConsistentItem(Object item) {
	            return true;
	         }
	      };
	   }

	@Override
	protected void fillContentProvider(AbstractContentProvider contentProvider,
		      ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
		         throws CoreException {
		
		QueryGateway gateway = new QueryGateway();		
		OMSResourceObject[] resourceObjects = gateway.getContent();  //Get the set of objects to be displayed.
		progressMonitor.beginTask("Searching", Array.getLength(resourceObjects)); //$NON-NLS-1$
		OMSLableProvider listLabelProvider = new OMSLableProvider(); 
		this.setListLabelProvider(listLabelProvider);
		
		//Pass the objects to the content provider
		for(int i=0;i<Array.getLength(resourceObjects);i++){
			contentProvider.add(resourceObjects[i], itemsFilter);
		}
		progressMonitor.done();		
	 }
	
	private static final String DIALOG_SETTINGS = "FilteredResourcesSelectionDialogExampleSettings";	
	@Override
	protected IDialogSettings getDialogSettings() {
	      	IDialogSettings settings = Activator.getDefault().getDialogSettings()
					.getSection(DIALOG_SETTINGS);
			if (settings == null) {
				settings = Activator.getDefault().getDialogSettings()
						.addNewSection(DIALOG_SETTINGS);
			}
			return settings;
	   }

	@Override
	public String getElementName(Object item) {
		return item.toString();
	}

	@Override
	 protected Comparator getItemsComparator() {
	      return new Comparator() {
	         public int compare(Object arg0, Object arg1) {
	            return arg0.toString().compareTo(arg1.toString());
	         }
	      };
	   }

	@Override
	 protected IStatus validateItem(Object item) {		
	      return Status.OK_STATUS;
	   }
	
	@Override
	protected void okPressed() {
		ResourceLoader loader = new ResourceLoader();
		loader.openResource(this.getSelectedItems());
		super.okPressed();
	}
}
